<template>
  <div class="app-container">
    <!--查询表单-->
    <el-form :inline="true" align="center">
      <el-form-item>
        <el-input
          v-model="searchObj.vaccineName"
          class="inline-input"
          placeholder="疫苗名称"
        />
      </el-form-item>
      <el-radio v-model="searchObj.isState" label="4">只查看有反馈</el-radio>
      <el-radio v-model="searchObj.vaccineName" label="2">查看全部已完成接种信息</el-radio>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="fetchData()">查询</el-button>
        <el-button type="default" @click="resetData()">清空</el-button>
      </el-form-item>

    </el-form>
    <!-- 工具按钮 -->
    <div style="margin-bottom: 10px">
      <el-button :loading="downloadLoading" style="margin:0 0 20px 20px;" type="primary" icon="el-icon-document" @click="handleDownload">
        导出为excel
      </el-button>
    </div>
    <!-- 表格 -->
    <el-table :data="list " border stripe>
      <el-table-column v-if="false" width="70" align="center" label="#">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column prop="vaccinatorName" label="疫苗接种人" width="180" align="center" />
      <el-table-column prop="vaccineName" label="疫苗名称" width="300" align="center" />
      <el-table-column prop="hospitalName" label="接种地点" width="180" align="center" />
      <el-table-column prop="vaccineIdcard" label="疫苗信息码" width="360" align="center" />
      <el-table-column prop="feedback" label="反馈信息" width="520" align="center" />
    </el-table>
    <!-- 分页组件 -->
    <el-pagination
      :current-page="page"
      :total="total"
      :page-size="limit"
      :page-sizes="[5, 10, 20, 30, 40, 50, 100,1000]"
      style="padding: 30px 0; text-align: center;"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="changePageSize"
      @current-change="changeCurrentPage"
    />
  </div>
</template>

<script>
import appointment from '@/api/appointment'
import { parseTime } from '@/utils'

export default {
  name: 'List',
  data() {
    return {
      list: null, // 数据列表
      total: 0, // 总记录数
      page: 1, // 页码
      limit: 10, // 每页记录数
      searchObj: {
      } // 查询条件
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      // 调用api
      appointment.pageList(this.page, this.limit, this.searchObj)
        .then(response => {
          this.list = response.data.rows
          this.total = response.data.total
        })
    },
    // 每页记录数改变，size：回调参数，表示当前选中的“每页条数”
    changePageSize(size) {
      this.limit = size
      this.fetchData()
    },
    // 改变页码，page：回调参数，表示当前选中的“页码”
    changeCurrentPage(page) {
      this.page = page
      this.fetchData()
    },
    // 重置表单
    resetData() {
      this.searchObj = {}
      this.fetchData()
    },
    handleDownload() {
      this.downloadLoading = true
      import('@/vendor/Export2Excel').then(excel => {
        const tHeader = ['接种人姓名', '疫苗名称', '接种医院', '反馈信息']
        const filterVal = ['vaccinatorName', 'vaccineName', 'hospitalName', 'feedback']
        const list = this.list
        const data = this.formatJson(filterVal, list)
        excel.export_json_to_excel({
          header: tHeader,
          data,
          filename: this.filename,
          autoWidth: this.autoWidth,
          bookType: this.bookType
        })
        this.downloadLoading = false
      })
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => {
        if (j === 'timestamp') {
          return parseTime(v[j])
        } else {
          return v[j]
        }
      }))
    }
  }
}
</script>

<style scoped>

</style>
